package com.ks.winterwarmimage.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;

import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.ks.winterwarmimage.R;

import java.io.IOException;
import java.util.Locale;

public class TemperatureArcView extends View {
    private final static String TAG="TemperatureArcView";
    /**起始角度*/
    private static final int START_ANGLE = 135;
    /**扫过的角度*/
    private static final int SWEEP_ANGLE = 270;
    /**背景圆弧画刷*/
    private Paint arcPaint;
    /**区间进度画刷*/
    private Paint rangePaint;
    /**滑块画刷*/
    private Paint thumbPaint;
    /**文字画刷*/
    private Paint  textPaint;
    /**背景圆弧绘制区域*/
    private RectF arcRect;
    /**圆心*/
    private PointF center;
    /**温度图标*/
    private Bitmap tempIcon;
    /**图标画刷*/
    private Paint iconPaint;
    // 温度范围文字
    private Paint rangeTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    /**圆弧半径*/
    private float radius;
    /**当前温度*/
    private float currentTemp = 0;
    /**是否正在拖动最小温度滑块*/
    private boolean draggingMin = false;
    /**是否正在拖动最大温度滑块*/
    private boolean draggingMax = false;
    /**滑块当前图层 0 最小温度滑块；1 最大温度滑块*/
    private int thumbLayer=0;
    /**是否有过滤色*/
    private  boolean isTint=false;
    ///////////////////////////属性值///////////////////////////////
    /**温度背景色*/
    private int tempBackgroundColor=0xffdbdbdb;
    /**温度区间颜色*/
    private int tempRangeColor=0xff2196F3;
    /**滑块低温度颜色*/
    private int thumbMinTempColor=Color.BLUE;
    /**滑块高温度颜色*/
    private int thumbMaxTempColor=Color.RED;
    /**当前温度文字颜色*/
    private int textCurrentTempColor=Color.BLACK;
    /**温度范围文字颜色(含可设置区间)*/
    private int textRangeTempColor=0xff303030;
    /**图标过滤颜色*/
    private int iconTintColor=0;
    /**当前温度文字大小*/
    private float textCurrentTempSize;
    /**温度范围文字大小*/
    private float textRangeTempSize;
    /**图标大小*/
    private int iconSize;
    /**滑块半径*/
    private float thumbRadius;
    /**圆环宽度*/
    private float arcStrokeWidth;
    /**最小可显示温度*/
    private  int minTemp = 0;
    /**最大可显示温度*/
    private  int maxTemp = 100;
    /**区间最小温度*/
    private float minRangeTemp ;
    /**区间最大温度*/
    private float maxRangeTemp ;

    /**
     * 温度变化事件侦听器
     */
    public interface OnTemperatureChangeListener {
        /**
         * 区间温度改变
         * @param view 温度弧形
         * @param temp 温度
         * @param isMinRangeTemp 是否为最小温度
         * @param fromUser 是否是用户操作
         */
        void onRangeTemperatureChanged(TemperatureArcView view, float temp,boolean isMinRangeTemp, boolean fromUser);
        /**
         * 当前温度改变
         * @param temp 温度
         */
        void onCurrentChanged(TemperatureArcView view, float temp);
        /**
         * 开始滑动（通常区间温度变化触发）
         * @param view  温度弧形
         * @param isMinRangeTemp 是否为最小温度
         */
        void onStartTrackingTouch(TemperatureArcView view,boolean isMinRangeTemp);
        /**
         * 结束滑动（通常区间温度变化触发）
         * @param view  温度弧形
         * @param isMinRangeTemp 是否为最小温度
         */
        void onStopTrackingTouch(TemperatureArcView view,boolean isMinRangeTemp);
    }
    private OnTemperatureChangeListener mOnTemperatureChangeListener;


    public TemperatureArcView(Context context) {
        this(context, null);
    }
    public TemperatureArcView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    public TemperatureArcView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TemperatureArcView);
        tempBackgroundColor=typedArray.getColor(R.styleable.TemperatureArcView_tempBackgroundColor, tempBackgroundColor);
        tempRangeColor=typedArray.getColor(R.styleable.TemperatureArcView_tempRangeColor, tempRangeColor);
        thumbMinTempColor=typedArray.getColor(R.styleable.TemperatureArcView_thumbMinTempColor, thumbMinTempColor);
        thumbMaxTempColor=typedArray.getColor(R.styleable.TemperatureArcView_thumbMaxTempColor, thumbMaxTempColor);
        textCurrentTempColor=typedArray.getColor(R.styleable.TemperatureArcView_textCurrentTempColor, textCurrentTempColor);
        textRangeTempColor=typedArray.getColor(R.styleable.TemperatureArcView_textRangeTempColor, textRangeTempColor);
        textCurrentTempSize=typedArray.getDimension(R.styleable.TemperatureArcView_textCurrentTempSize,dpToPx(24));
        textRangeTempSize=typedArray.getDimension(R.styleable.TemperatureArcView_textRangeTempSize,dpToPx(12));
        iconSize=typedArray.getDimensionPixelSize(R.styleable.TemperatureArcView_iconSize, (int) dpToPx(24));
        thumbRadius=typedArray.getDimension(R.styleable.TemperatureArcView_thumbRadius, dpToPx(12));
        arcStrokeWidth=typedArray.getDimension(R.styleable.TemperatureArcView_arcStrokeWidth, dpToPx(8));
        minTemp=typedArray.getInt(R.styleable.TemperatureArcView_minTemp, minTemp);
        maxTemp=typedArray.getInt(R.styleable.TemperatureArcView_maxTemp, maxTemp);
        minRangeTemp=typedArray.getInt(R.styleable.TemperatureArcView_minRangeTemp,minTemp);
        maxRangeTemp=typedArray.getInt(R.styleable.TemperatureArcView_maxRangeTemp, maxTemp);
        isTint=typedArray.hasValue(R.styleable.TemperatureArcView_iconTintColor);
        if(isTint) {
            iconTintColor=typedArray.getColor(R.styleable.TemperatureArcView_iconTintColor, iconTintColor);
        }
        if(typedArray.hasValue(R.styleable.TemperatureArcView_srcCompat)){
            int resId=typedArray.getResourceId(R.styleable.TemperatureArcView_srcCompat, 0);
            if(resId!=0) {
                tempIcon = VectorDrawableToBitmap(resId);
                if (this.tempIcon == null) {
                    // 温度图标初始化
                    tempIcon = BitmapFactory.decodeResource(getResources(),resId);
                }
            }
        }
        init(typedArray);
        typedArray.recycle();
    }
    private void init(TypedArray typedArray) {
        // 圆弧绘制配置
        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  // 抗锯齿
        arcPaint.setStyle(Paint.Style.STROKE);        // 画笔样式：空心
        arcPaint.setStrokeCap(Paint.Cap.ROUND);       // 画笔笔触样式：圆形
        arcPaint.setStrokeWidth(arcStrokeWidth);      // 画笔宽度
        arcPaint.setColor(tempBackgroundColor);       // 画笔颜色
        //温度区间
        rangePaint = new Paint(arcPaint);
        rangePaint.setColor(tempRangeColor);

        // 滑块样式
        thumbPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        thumbPaint.setStyle(Paint.Style.FILL);
        //thumbPaint.setColor(Color.parseColor("#2196F3"));

        // 温度文字
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(textCurrentTempSize);
        textPaint.setColor(textCurrentTempColor);
        textPaint.setTextAlign(Paint.Align.CENTER);
        // 设置当前温度文本的字体和样式
        String fontFamilyCurrentTemp = typedArray.getString(R.styleable.TemperatureArcView_fontFamilyCurrentTemp);
        int typefaceCurrentTemp = typedArray.getInt(R.styleable.TemperatureArcView_typefaceCurrentTemp, Typeface.NORMAL);
        int textStyleCurrentTemp = typedArray.getInt(R.styleable.TemperatureArcView_textStyleCurrentTemp, Typeface.NORMAL);
        Typeface currentTempTypeface = Typeface.create(fontFamilyCurrentTemp, typefaceCurrentTemp);
        textPaint.setTypeface(Typeface.create(currentTempTypeface, textStyleCurrentTemp));

        //图标画刷
        iconPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        iconPaint .setStyle( Paint.Style.STROKE );
        iconPaint.setColorFilter(new PorterDuffColorFilter(iconTintColor, PorterDuff.Mode.SRC_IN));

         // 温度范围文字样式
        rangeTextPaint.setTextSize(textRangeTempSize);
        rangeTextPaint.setTextAlign(Paint.Align.CENTER);
        rangeTextPaint.setColor(textRangeTempColor);
        String fontFamilyRangeTemp = typedArray.getString(R.styleable.TemperatureArcView_fontFamilyRangeTemp);
        int typefaceRangeTemp = typedArray.getInt(R.styleable.TemperatureArcView_typefaceRangeTemp, Typeface.NORMAL);
        int textStyleRangeTemp = typedArray.getInt(R.styleable.TemperatureArcView_textStyleRangeTemp, Typeface.NORMAL);
        Typeface rangeTempTypeface = Typeface.create(fontFamilyRangeTemp, typefaceRangeTemp);
        rangeTextPaint.setTypeface(Typeface.create(rangeTempTypeface, textStyleRangeTemp));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float padding = thumbRadius+dpToPx(2);
        center=new PointF(getWidth()/2, getHeight()/2);
        radius=Math.min(getWidth(), getHeight())/2-padding;
        float drawLeft=center.x-radius;
        float drawTop=center.y-radius;
        float drawRight=center.x+radius;
        float drawBottom=center.y+radius;
        arcRect = new RectF(drawLeft, drawTop, drawRight, drawBottom);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制背景圆弧
        canvas.drawArc(arcRect, START_ANGLE, SWEEP_ANGLE, false, arcPaint);
        // 绘制有效温度区间
        drawRangeArc(canvas);
        // 绘制滑块
        if(draggingMin){
            drawThumb(canvas, maxRangeTemp, thumbMaxTempColor);
            drawThumb(canvas, minRangeTemp, thumbMinTempColor);
        }else{
            drawThumb(canvas, minRangeTemp, thumbMinTempColor);
            drawThumb(canvas, maxRangeTemp, thumbMaxTempColor);
        }
        // 绘制中央显示内容
        drawCenterText(canvas);
        drawDisplayRangeText(canvas);
    }
    /**
     * 绘制有效温度区间
     * @param canvas
     */
    private void drawRangeArc(Canvas canvas) {
        float startAngle = tempToAngle(minRangeTemp);
        float sweepAngle = tempToAngle(maxRangeTemp) - startAngle;
        canvas.drawArc(arcRect, startAngle-90, sweepAngle, false, rangePaint);
    }
    /**
     * 绘制滑块
     * @param canvas
     * @param temp
     * @param color
     */
    private void drawThumb(Canvas canvas, float temp, int color) {
        float angle = tempToAngle(temp);
        PointF pos = angleToPosition(angle);
        thumbPaint.setColor(color);
        canvas.drawCircle(pos.x, pos.y, thumbRadius, thumbPaint);
    }
    /**
     * 绘制中心内容
     * @param canvas
     */
    private void drawCenterText(Canvas canvas) {
        // 绘制当前温度
        String currentTempText = String.format("%.1f℃", currentTemp);
        Rect bounds = new Rect();
        textPaint.getTextBounds(currentTempText, 0, currentTempText.length(), bounds);   //返回文本边界
        float y = center.y - (bounds.top + bounds.bottom)/2f;
        canvas.drawText(currentTempText, center.x, y, textPaint);
        if(tempIcon!=null) {
            // 绘制温度图标
            float iconX=center.x-iconSize/2f;
            float iconY = center.y - iconSize - bounds.height(); // 调整图标垂直位置
            RectF rect = new RectF(iconX,iconY,iconX+iconSize,iconY+iconSize);
            canvas.drawBitmap(tempIcon, null,rect, isTint?iconPaint:null);
        }
        // 绘制温度范围
        String rangeText = String.format("%.0f℃ / %.0f℃", minRangeTemp, maxRangeTemp);
        float rangeY = center.y + dpToPx(24)+bounds.height()/2f; // 调整范围文本位置
        canvas.drawText(rangeText, center.x, rangeY, rangeTextPaint);
    }
    /**
     * 绘制可显示的温度范围值
     * @param canvas
     */
    private void drawDisplayRangeText(Canvas canvas) {
        Rect bounds = new Rect();
        float angle = tempToAngle(minTemp);
        PointF pos = angleToPosition(angle);
        String minText = String.format("%.0f℃", (float)minTemp);
        String maxText = String.format("%.0f℃", (float)maxTemp);
        //为保证来昂贵部分的字体行高，将两部分内容合并计算绘制区域
        rangeTextPaint.getTextBounds(minText+maxText, 0, minText.length(), bounds);
        float text_y = pos.y+bounds.height()+4+thumbRadius;
        canvas.drawText(minText, pos.x, text_y, rangeTextPaint);
        angle = tempToAngle(maxTemp);
        pos = angleToPosition(angle);
        rangeTextPaint.getTextBounds(maxText, 0, minText.length(), bounds);
        canvas.drawText(maxText, pos.x, text_y, rangeTextPaint);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        checkThumbTouch(x, y);
        if (draggingMin || draggingMax) {  //如果触摸坐标在圆环或颜色选择器上时
            getParent().requestDisallowInterceptTouchEvent(true); //请求请求所有的视图件禁止接收触摸事件
        }
        return super.dispatchTouchEvent(event);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                checkThumbTouch(x, y);
                if(mOnTemperatureChangeListener!=null) {
                    if (draggingMin || draggingMax) {
                        mOnTemperatureChangeListener.onStartTrackingTouch(this, draggingMin ? true : false);
                    }
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                updateThumbPosition(x, y);
                if(mOnTemperatureChangeListener!=null) {
                    if(draggingMin){
                        mOnTemperatureChangeListener.onRangeTemperatureChanged(this, minRangeTemp, true, true);
                    }else if(draggingMax){
                        mOnTemperatureChangeListener.onRangeTemperatureChanged(this, maxRangeTemp, false, true);
                    }
                }
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                if(mOnTemperatureChangeListener!=null) {
                    if (draggingMin || draggingMax) {
                        mOnTemperatureChangeListener.onStopTrackingTouch(this, draggingMin ? true : false);
                    }
                }
                draggingMin = draggingMax = false;
                return true;
        }
        return super.onTouchEvent(event);
    }
    /**
     * 将温度转换为角度
     * @param temp
     * @return
     */
    private float tempToAngle(float temp) {
        float ratio = (temp - minTemp) / (maxTemp - minTemp);
        return START_ANGLE + SWEEP_ANGLE*ratio+90;
    }
    /**
     * 将角度转换为温度
     * @param angle 角度
     * @return 温度
     */
    private float angleToTemp(float angle) {
        if(angle<90||angle>=START_ANGLE) {
            if (angle < START_ANGLE) angle += 360;
            float ratio = (angle - START_ANGLE) / SWEEP_ANGLE;
            return minTemp + (maxTemp - minTemp) * ratio;
        }else{
            float ratio= SWEEP_ANGLE/(maxTemp - minTemp);
            return (angle-START_ANGLE)/ratio;
        }
    }
    /**
     * 将角度转换为坐标
     * @param angle
     * @return
     */
    private PointF angleToPosition(float angle) {
        float thumb_x = (float) (center.x + radius* Math.sin(angle * Math.PI / 180));
        float thumb_y = (float) (center.y - radius * Math.cos(angle * Math.PI / 180));
        return new PointF(thumb_x, thumb_y);
    }
    /**
     * 检查是否点击滑块
     * @param x
     * @param y
     */
    private void checkThumbTouch(float x, float y) {
        PointF minPos = angleToPosition(tempToAngle(minRangeTemp));
        if (distance(x, y, minPos.x, minPos.y) < thumbRadius * 2) {
            draggingMin = true;
            //return;
        }

        PointF maxPos = angleToPosition(tempToAngle(maxRangeTemp));
        if (distance(x, y, maxPos.x, maxPos.y) < thumbRadius * 2) {
            draggingMax = true;
        }
        if(draggingMin && draggingMax){
            if(thumbLayer==0){
                draggingMax=false;
            }else{
                draggingMin=false;
            }
        }
        thumbLayer=draggingMax?1:0;
    }
    /**
     * 更新滑块位置
     * @param x
     * @param y
     */
    private void updateThumbPosition(float x, float y) {
        float angle = positionToAngle(x, y);
        float temp = angleToTemp(angle);
        if (draggingMin) {
            float minVal=Math.min(temp, maxRangeTemp);
            minRangeTemp = Math.max(minTemp, minVal);
        } else if (draggingMax) {
            maxRangeTemp = Math.min(maxTemp, Math.max(temp, minRangeTemp));
        }
    }

    /**
     * 将坐标点转换为角度
     * @param x 坐标点
     * @param y
     * @return
     */
    private float positionToAngle(float x, float y) {
        float centerX = getWidth()/2f;
        float centerY = getHeight()/2f;
        float dx = x - centerX;
        float dy = y - centerY;

        double radian = Math.atan2(dy, dx);
        float angle = (float) Math.toDegrees(radian);
        return (angle + 360) % 360; // 转换为0-360度
    }
    /**
     * DP转像像素
     * @param dp 需要转换的dp值
     * @return 像素
     */
    private float dpToPx(float dp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
/**
     * 计算两点之间的距离
     * @param x1 点1的x坐标
     * @param y1 点1的y坐标
     * @param x2 点2的x坐标
     * @param y2 点2的y坐标
     * @return 两点之间的距离
     */
    private float distance(float x1, float y1, float x2, float y2) {
        return (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }

    /**
     * Drawable转位图
     * @param drawable
     * @return
     */
    private Bitmap DrawableToBitmap(Drawable drawable){
        BitmapDrawable bd=null;
        try {
            bd = (BitmapDrawable) drawable;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(bd!=null) {
            return bd.getBitmap();
        }else{
            int width = drawable.getIntrinsicWidth();
            int height = drawable.getIntrinsicHeight();
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0,0,width,height);
            drawable.draw(canvas);
            return bitmap;
        }
    }

    /**
     * 从资源ID中的矢量图转位图
     * @param resId 资源id
     * @return
     */
    private Bitmap VectorDrawableToBitmap(int resId){
        Drawable drawable = ContextCompat.getDrawable(getContext(),resId);
        drawable = (DrawableCompat.wrap(drawable)).mutate();
        return DrawableToBitmap(drawable);
    }

    //----------------------------------公开属性--------------------------//

    public void  setTempBackgroundColor(int color){
        tempBackgroundColor=color;
        arcPaint.setColor(tempBackgroundColor);
        invalidate();
    }
    public int getTempBackgroundColor(){
        return tempBackgroundColor;
    }
    public void  setTempRangeColor(int color){
        tempRangeColor=color;
        rangePaint.setColor(tempRangeColor);
        invalidate();
    }
    public int getTempRangeColor(){
        return tempRangeColor;
    }
    public void  setThumbMinTempColor(int color){
        thumbMinTempColor=color;
        //thumbPaint.setColor(thumbLowTempColor);
        invalidate();
    }
    public int getThumbMinTempColor(){
        return thumbMinTempColor;
    }
    public void setThumbMaxTempColor(int color){
        thumbMaxTempColor=color;
        invalidate();
    }
    public int getThumbMaxTempColor(){
        return thumbMaxTempColor;
    }
    public void setTextCurrentTempColor(int color){
        textCurrentTempColor=color;
        textPaint.setColor(textCurrentTempColor);
        invalidate();
    }
    public int getTextCurrentTempColor(){
        return textCurrentTempColor;
    }
    public void setTextRangeTempColor(int color){
        textRangeTempColor=color;
        rangeTextPaint.setColor(textRangeTempColor);
        invalidate();
    }
    public int getTextRangeTempColor(){
        return textRangeTempColor;
    }
    public void setIconTintColor(int color){
        iconTintColor=color;
        iconPaint.setColorFilter(new PorterDuffColorFilter(iconTintColor, PorterDuff.Mode.SRC_IN));
        isTint=color!=Color.TRANSPARENT;
        invalidate();
    }
    public int getIconTintColor(){
        return iconTintColor;
    }
    public void setTextCurrentTempSize(float size){
        textCurrentTempSize=size;
        textPaint.setTextSize(textCurrentTempSize);
        invalidate();
    }
    public float getTextCurrentTempSize(){
        return textCurrentTempSize;
    }
    public void setTextRangeTempSize(float size){
        textRangeTempSize=size;
        rangeTextPaint.setTextSize(textRangeTempSize);
        invalidate();
    }
    public float getTextRangeTempSize(){
        return textRangeTempSize;
    }
    public void setIconSize(int size){
        iconSize=size;
        invalidate();
    }
    public int getIconSize(){
        return iconSize;
    }
    public void setThumbRadius(float radius){
        thumbRadius=radius;
        invalidate();
    }
    public float getThumbRadius(){
        return thumbRadius;
    }
    public void setArcStrokeWidth(float width){
        arcStrokeWidth=width;
        arcPaint.setStrokeWidth(arcStrokeWidth);
        invalidate();
    }
    public float getArcStrokeWidth(){
        return arcStrokeWidth;
    }
    /**
     * 设置当前温度
     * @param temp 当前温度
     */
    public void setCurrentTemperature(float temp) {
        setCurrentTemperature(temp,true);
    }
    /**
     * 获取当前温度
     * @param temp 当前温度
     * @param isRefresh 是否刷新
     */
    public void setCurrentTemperature(float temp, boolean isRefresh) {
        currentTemp = temp;
        mOnTemperatureChangeListener.onCurrentChanged(this,currentTemp);
        if(isRefresh)
            invalidate();
    }
    public float getCurrentTemperature() {
        return currentTemp;
    }
    /**
     * 设置最小温度
     * @param minRangeTemp 最小温度
     */
    public void setMinRangeTemperature(float minRangeTemp) {
        setMinRangeTemperature(minRangeTemp,true);
    }
    /**
     * 设置最小温度
     * @param minRangeTemp 最小温度
     * @param isRefresh 是否刷新
     */
    public void setMinRangeTemperature(float minRangeTemp, boolean isRefresh) {
        this.minRangeTemp = minRangeTemp;
        if(isRefresh)
            invalidate();
        if(mOnTemperatureChangeListener!=null){
            mOnTemperatureChangeListener.onRangeTemperatureChanged(this,minRangeTemp,true,false);
        }
    }
    /**
     * 获取最小温度
     * @return 最小温度
     */
    public float getMinRangeTemperature() {
        return minRangeTemp;
    }
    /**
     * 获取最大温度
     * @return 最大温度
     */
    public float getMaxRangeTemperature() {
        return maxRangeTemp;
    }
    /**
     * 设置最大温度
     * @param maxRangeTemp 最大温度
     */
    public void setMaxRangeTemperature(float maxRangeTemp) {
        setMaxRangeTemperature(maxRangeTemp,true);
    }
    /**
     * 设置最大温度
     * @param maxRangeTemp 最大温度
     * @param isRefresh 是否刷新
     */
    public void setMaxRangeTemperature(float maxRangeTemp, boolean isRefresh) {
        this.maxRangeTemp = maxRangeTemp;
        if(isRefresh)
            invalidate();
        if(mOnTemperatureChangeListener!=null){
            mOnTemperatureChangeListener.onRangeTemperatureChanged(this,maxRangeTemp,false,false);
        }
    }
     /**
     * 设置最大温度
     * @param maxTemp 最大温度
     */
    public void setMaxTemperature(int maxTemp) {
        setMaxTemperature(maxTemp,true);
    }
    /**
     * 设置最大温度
     * @param maxTemp 最大温度
     * @param isRefresh 是否刷新
     */
    public void setMaxTemperature(int maxTemp,  boolean isRefresh) {
        this.maxTemp = maxTemp;
        if(isRefresh)
             invalidate();
    }
    /**
     * 获取最大温度
     * @return 最大温度
     */
    public int getMaxTemperature() {
        return maxTemp;
    }

    public void setMinTemperature(int minTemp) {
        setMinTemperature(minTemp,true);
    }
    public void setMinTemperature(int minTemp, boolean isRefresh) {
        this.minTemp = minTemp;
        if(isRefresh)
            invalidate();
    }
    public int getMinTemperature() {
        return minTemp;
    }
    public void setIcon(int resId){
        tempIcon = VectorDrawableToBitmap(resId);
        if (this.tempIcon == null) {
            tempIcon = BitmapFactory.decodeResource(getResources(),resId);
        }
        invalidate();
    }
    public void setIcon(Bitmap bitmap){
        this.tempIcon = bitmap;
        invalidate();
    }
    /**
     * 设置图片图标
     * @param icon 图标
     */
    public void setIcon(Icon icon){
        if ( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M ) {
            BitmapDrawable bd=(BitmapDrawable) icon.loadDrawable(this.getContext());
            setIcon(bd.getBitmap());
        }
    }
    /**
     * 设置图片uri地址
     * @param uri
     */
    public void setIcon(Uri uri){
        try {
            Bitmap bm= MediaStore.Images.Media.getBitmap(this.getContext().getContentResolver(), uri);
            setIcon(bm);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG,e.getMessage());
        }
    }
    public Bitmap getIcon(){
        return tempIcon;
    }
    public void setOnTemperatureChangeListener(OnTemperatureChangeListener listener){
        mOnTemperatureChangeListener=listener;
    }
    public void setCurrentTempFontFamily(String fontFamily) {
        Typeface typeface = Typeface.create(fontFamily, textPaint.getTypeface().getStyle());
        textPaint.setTypeface(typeface);
        invalidate();
    }

    public void setCurrentTempTextStyle(int textStyle) {
        Typeface typeface = Typeface.create(textPaint.getTypeface(), textStyle);
        textPaint.setTypeface(typeface);
        invalidate();
    }

    public void setRangeTempFontFamily(String fontFamily) {
        Typeface typeface = Typeface.create(fontFamily, rangeTextPaint.getTypeface().getStyle());
        rangeTextPaint.setTypeface(typeface);
        invalidate();
    }

    public void setRangeTempTextStyle(int textStyle) {
        Typeface typeface = Typeface.create(rangeTextPaint.getTypeface(), textStyle);
        rangeTextPaint.setTypeface(typeface);
        invalidate();
    }

}
